<template>
  <div>
    <!-- 卡片视图区 -->
    <el-card class="box-card">
      <!-- 面包屑导航区 -->
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>菜单管理</el-breadcrumb-item>
        <el-breadcrumb-item>菜单设置</el-breadcrumb-item>
      </el-breadcrumb>

      <!-- 搜索区 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            placeholder="请输入搜索内容"
            clearable
            v-model="queryInfo.query">
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="searchMenu">
            </el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addClick">添加子菜单</el-button>
        </el-col>
      </el-row>

      <!-- 管理员表格区 -->
      <el-table
        :header-cell-style="{textAlign: 'center'}"
        :cell-style="{ textAlign: 'center' }"
        :data="tableData"
        border
        style="width: 100%">
        <el-table-column
          prop="id"
          label="序号"
          width="180">
        </el-table-column>
        <el-table-column
          prop="mid"
          label="主菜单id"
          width="180">
        </el-table-column>
        <el-table-column
          prop="title"
          label="子菜单名"
          width="180">
        </el-table-column>
        <el-table-column
          prop="path"
          label="路径">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button 
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="editClick(scope.row)"></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pageNum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background>
      </el-pagination>
    </el-card>

    <!-- 操作子菜单对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="opeDialogVisible"
      width="50%">
      <el-form
        :model="opeForm"
        :rules="opeFormRules"
        ref="opeFormRef"
        label-width="100px">
        <el-form-item label="序号" prop="id">
          <el-input v-model="opeForm.id"></el-input>
        </el-form-item>
        <el-form-item label="主菜单序号" prop="mid">
          <el-input v-model="opeForm.mid"  :disabled='isDisabled'></el-input>
        </el-form-item>
        <el-form-item label="子菜单名" prop="title">
          <el-input v-model="opeForm.title"></el-input>
        </el-form-item>
        <el-form-item label="路径" prop="path">
          <el-input v-model="opeForm.path"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="opeDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="operation">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  created() {
    this.getMenus()
  },
  data() {
    return {
      // 获取所有子菜单的查询参数
      queryInfo: {
        pageNum: 1,
        pageSize: 10,
        query: ""
      },
      total: 0,
      // 子菜单数据
      tableData: [],
      // 修改子菜单
      opeDialogVisible: false,
      opeForm: {
        id: '',
        mid: '',
        path: '',
        title: ''
      },
      opeFormRules: {
        path: [
          { required: true, message: '路径是必填项', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        title: [
          { required: true, message: '子菜单名是必填项', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ]
      },
      title: '',
      isDisabled: false
    }
  },
  methods: {
    async getMenus() {
      const { data: res } = await this.$http.post('getSubMenus', this.queryInfo)
      if (res.code != 'success') return this.$message.error('获取子菜单列表失败')
      this.tableData = res.data
      this.total = res.numbers
      // console.log(this.tableData)
    },
    editClick(menu) {
      this.title = '修改子菜单信息'
      this.opeForm.id = menu.id
      this.opeForm.mid = menu.mid
      this.opeForm.title = menu.title
      this.opeForm.path = menu.path
      this.isDisabled = true
      this.opeDialogVisible = true
    },
    addClick() {
      this.title = '添加子菜单'
      this.opeForm.id = ''
      this.opeForm.mid = ''
      this.opeForm.title = ''
      this.opeForm.path = ''
      this.isDisabled = false
      this.opeDialogVisible = true
    },
    async operation() {
      if (this.title === '修改子菜单信息') {
        this.$refs.opeFormRef.validate(async valid => {
          if (!valid) return
          const { data: res } = await this.$http.post('updateSubMenus', this.opeForm)
          if (res !== 'success') return this.$message.error('修改管理员信息失败')
          this.$message.success('修改子菜单信息成功')
          this.opeDialogVisible = false
          this.getMenus()
        })
      } else {
        const { data: res } = await this.$http.post('insertSubMenus', this.opeForm)
        if (res !== 'success') return this.$message.error('添加子菜单失败')
        this.$message.success('添加子菜单成功')
        this.opeDialogVisible = false
        this.getMenus()
      }
    },
    // 搜索菜单
    async searchMenu() {
      const { data: res } = await this.$http.post('getSubMenus', this.queryInfo)
      if (res.code !== 'success') return this.$message.error('子菜单搜索失败')
      this.$message.success('子菜单搜索成功')
      this.tableData = res.data
    },
    // 每页显示条数发生改变时
    handleSizeChange(size) {
      this.queryInfo.pageSize = size
      this.getMenus()
    },
    // 当前页码发生改变时
    handleCurrentChange(currentPage) {
      this.queryInfo.pageNum = currentPage
      this.getMenus()
    }
  }
}
</script>
